package com.dtw.ditouwang.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.ui.login.LoginActivity;

import java.util.ArrayList;
import java.util.List;

public class StartActivity extends BaseActivity implements View.OnClickListener {
    ImageView imageView;
    ViewPager viewPager;
    List<ImageView> imageViewList = new ArrayList<>();
    int currentPage = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        imageView = findViewById(R.id.img);
        viewPager = findViewById(R.id.view_page);
        new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long l) {
            }

            @Override
            public void onFinish() {
                if (sharedPreferencesUtil.isFirstIn()) {
                    imageView.setVisibility(View.GONE);
                } else {
                    startActivity(new Intent(StartActivity.this, LoginActivity.class));
                    finish();
                }
            }
        }.start();
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(imageView).load("http://www.ditouwang.net/public/static/yd/ad1.jpg").into(imageView);
        imageView.setOnClickListener(this);
        imageViewList.add(imageView);
        imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(imageView).load("http://www.ditouwang.net/public/static/yd/ad2.jpg").into(imageView);
        imageView.setOnClickListener(this);
        imageViewList.add(imageView);
        imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(imageView).load("http://www.ditouwang.net/public/static/yd/ad3.jpg").into(imageView);
        imageView.setOnClickListener(this);
        imageViewList.add(imageView);
        viewPager.setAdapter(new StartAdapter());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                currentPage = i;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        viewPager.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (currentPage == 2) {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
    }

    class StartAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageViewList.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
            return view == o;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(imageViewList.get(position));
            return imageViewList.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(imageViewList.get(position));
        }
    }
}
